package cz.muni.fi.pa165.ws.adapter.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;

import cz.muni.fi.pa165.sme.api.SportActivityService;
import cz.muni.fi.pa165.sme.api.to.SportActivityTO;
import cz.muni.fi.pa165.sportmanager.sportactivityws.SportActivity;
import cz.muni.fi.pa165.ws.adapter.ISportActivityAdapter;
import cz.muni.fi.pa165.ws.converter.SportActivityConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * @author forman
 */
@Component
public class SportActivityAdapter implements ISportActivityAdapter {

	@Autowired
	private SportActivityService sportActivityService;

	@Autowired
	private SportActivityConverter converter;

	@Override
	public void createSportActivity(final SportActivity sportActivity) {
		final SportActivityTO sportActivityTO = converter.convert2SportActivityTO(sportActivity);
		sportActivityService.createSportActivity(sportActivityTO);
	}

	@Override
	public void deleteSportActivity(final SportActivity sportActivity) {
		final SportActivityTO sportActivityTO = converter.convert2SportActivityTO(sportActivity);
		sportActivityService.deleteSportActivity(sportActivityTO);
	}

	@Override
	public void updateSportActivity(final SportActivity sportActivity) {
		final SportActivityTO sportActivityTO = converter.convert2SportActivityTO(sportActivity);
		sportActivityService.updateSportActivity(sportActivityTO);
	}

	@Override
	public SportActivity findSportActivity(final Long id) {
		final SportActivityTO sportActivityById = sportActivityService.findSportActivityById(id);
		return converter.convert2SportActivity(sportActivityById);
	}

	@Override
	public List<SportActivity> findAllSportActivity() {
		final List<SportActivityTO> allSportActivity = sportActivityService.findAllSportActivities();
		final List<SportActivity> result = new ArrayList<SportActivity>();

		for (SportActivityTO sportActivityTO : allSportActivity) {
			result.add(converter.convert2SportActivity(sportActivityTO));
		}
		return result;
	}

	@PostConstruct
	private void init() {
		final List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("ADMIN"), new SimpleGrantedAuthority("USER"));
		final UserDetails userDetails = new User("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918", authorities);
		Authentication authentication = new AnonymousAuthenticationToken("ws", userDetails, authorities);
		SecurityContextHolder.getContext().setAuthentication(authentication);
	}
}
